package com.mediacloud.app.newsmodule.fragment.baoliao.activity;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.user.controller.BaseController;
import com.mediacloud.app.user.utils.RxUtils;
import com.mediacloud.app.view.XSmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.mediacloud.app.model.activity.BaseBackActivity;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.baoliao.TransUtils;
import com.mediacloud.app.newsmodule.fragment.baoliao.OnItemClickListener;
import com.mediacloud.app.newsmodule.fragment.baoliao.adapter.BaoLiaoVideoListDetailAdapter;
import com.mediacloud.app.model.news.BaoLiaoListModel;
import com.mediacloud.app.model.news.BaoLiaoMeta;
import com.mediacloud.app.reslib.util.DataInvokeUtil;

import java.util.ArrayList;
import java.util.List;

public class BaoLiaoVideoListDetailActivity extends BaseBackActivity implements OnItemClickListener, OnRefreshListener, OnLoadMoreListener {
    private XSmartRefreshLayout ptrLayout;
    private RecyclerView recyclerView;
    List<BaoLiaoMeta> list;
    BaoLiaoMeta meta;
    BaoLiaoVideoListDetailAdapter adapter;
    private int page = 1;
    private int perPage = 20;
    private int catalog_id;

    @Override
    public int getLayoutResID() {
        return R.layout.activity_bao_liao_video_list_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = new ArrayList<>();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            meta = bundle.getParcelable("meta");
            catalog_id = bundle.getInt("catalog_id");
            setTitle(meta.getCatalogName());
            list.add(meta);
        }
        initView();
    }

    private void initView() {
        recyclerView = findViewById(R.id.mRecyclerView);
        recyclerView.setItemAnimator(null); //置空缺陷忽略
        ptrLayout = findViewById(R.id.ptrLayout);
        ptrLayout.setEnableRefresh(false);
        ptrLayout.setEnableLoadMore(true);
        ptrLayout.setOnRefreshListener(this);
        ptrLayout.setOnLoadMoreListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new BaoLiaoVideoListDetailAdapter(this, recyclerView);
        recyclerView.setAdapter(adapter);
        adapter.setList(list);
        getListData();
    }


    @Override
    public void onClick(int position) {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        ptrLayout.setEnableLoadMore(false);
        ptrLayout.setEnableRefresh(false);
        page++;
        getListData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        ptrLayout.setEnableLoadMore(false);
        ptrLayout.setEnableRefresh(false);
        page = 1;
        getListData();
    }

    private void getListData() {
        if(meta == null){
            return;
        }
        DataInvokeUtil.getZiMeiTiApi().getTipOffAbout(catalog_id, (int) meta.getId(),page, perPage).compose(TransUtils.fastJSonTransform(BaoLiaoListModel.class))
                .compose(RxUtils.<BaoLiaoListModel>schedulersTransformer())
                .subscribe(new BaseController().new BaseObserver<BaoLiaoListModel>() {
                    @Override
                    public void onNext(BaoLiaoListModel model) {
                        ptrLayout.finishRefresh();
                        ptrLayout.finishLoadMore();
                        try {
                            if (model.isState()) {
                                boolean haveMore = model.getData().getPaging().getLastPage() > model.getData().getPaging().getCurrentPage();
                                ptrLayout.setEnableLoadMore(haveMore);
                                adapter.addList(model.getData().getMeta());
                            }
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        ptrLayout.setEnableLoadMore(true);
                        ptrLayout.setEnableRefresh(true);
                        ptrLayout.finishRefresh();
                        ptrLayout.finishLoadMore();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.destroy();
        }
    }
}